﻿using System;
using System.Collections.Generic;

namespace RexToy.Cassandra
{
    class LexicalUUIDType : IMarshal
    {
        public byte[] GetBytes(object data)
        {
            if (data == null)
                return Constant.Zero_Length_Byte_Array;

            Guid g = (Guid)data;
            var bytes = g.ToByteArray();
            MarshalUtil.AdjustBufferOrder(bytes, 0, 3);
            MarshalUtil.AdjustBufferOrder(bytes, 4, 5);
            MarshalUtil.AdjustBufferOrder(bytes, 6, 7);

            return bytes;
        }

        public object GetData(byte[] bytes)
        {
            if (bytes.Length == 0)
                return null;

            MarshalUtil.AdjustBufferOrder(bytes, 0, 3);
            MarshalUtil.AdjustBufferOrder(bytes, 4, 5);
            MarshalUtil.AdjustBufferOrder(bytes, 6, 7);

            return new Guid(bytes);
        }
    }
}
